export default function fetchApp(uri, params, postbody, options = {}) {
  const { type } = options;

  const reqHeaders = {
    'Content-Type': 'application/json; charset=utf-8',
    'Request-Type': 'json'
  };

  const requestObj = {
    method: type,
    headers: reqHeaders,
    credentials: 'same-origin'
  };

  const parser = res => res.json();

  const request = new Request(uri, requestObj);

  return fetch(request).then((res) => {
    const { status, statusText } = res;

    if (status >= 200 && status < 300) {
      return parser(res);
    }

    const errorObj = {
      status: 'error',
      code: status,
      message: statusText
    }

    throw errorObj;
  });
}
